package yxy.game.pm2.command.plainsCraft;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import yxy.cherry.battle.module.Position;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Format;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PlainsCraftMessage;
import yxy.game.pm2.module.hero.FormatHandle;
import yxy.game.pm2.module.plainsCraft.PlainsCraftHandle;
import yxy.game.pm2.module.plainsCraft.PlainsCraftPlayerModule;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

/** 中原争霸 中原争霸布阵界面 */
@MessageType(proto = Command.PlainsCraftFormatUI, classtype = PlainsCraftMessage.PlainsCraftFormatUIRequest.class)
public class CMD_PlainsCraftFormatUI extends Command<PlainsCraftMessage.PlainsCraftFormatUIRequest> {

	@Override
	public void execute(MessageSource messageSource, PlainsCraftMessage.PlainsCraftFormatUIRequest request) {
        Date date = new Date();
        try {
            String playerCode = messageSource.getTag();

            PlainsCraftMessage.PlainsCraftFormatUIResponse.Builder response = PlainsCraftMessage.PlainsCraftFormatUIResponse.newBuilder();

            Result result = new PlainsCraftHandle().formatInfo(playerCode, response);

            send(messageSource, response, result);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Date date1 = new Date();
        System.out.println("CMD_PlainsCraftFormatUI:"+(date1.getTime()-date.getTime()));

    }


    private JSONArray getPower(String playerCode){
        //给3个队伍每个队伍的英雄战力
        FormatHandle formatHandle = new FormatHandle();
        JSONArray returnArrary = new JSONArray();
        PlainsCraftPlayerModule playerModule = PlainsCraftPlayerModule.get(playerCode);
        ArrayList<Format> formatArrayList = playerModule.getFormat();
        for (Format format : formatArrayList) {
            JSONArray formatHeroArrary = new JSONArray();
            Map<Position, Hero> map = formatHandle.formatHeros(playerCode, format);
            for (Map.Entry<Position, Hero> entry : map.entrySet()) {
                JSONObject heroJson = new JSONObject();
                Hero hero = entry.getValue();
                heroJson.put("code",hero.getCode());
                heroJson.put("power",hero.getPower());
                formatHeroArrary.add(heroJson);
            }
            returnArrary.add(formatHeroArrary);
        }
	    return returnArrary;
    }

	private void send(MessageSource messageSource, PlainsCraftMessage.PlainsCraftFormatUIResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}